import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'privacy_logic.dart';
import 'privacy_state.dart';

class PrivacyPage extends StatelessWidget {
  const PrivacyPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final PrivacyLogic logic = Get.put(PrivacyLogic());
    final PrivacyState state = Get.find<PrivacyLogic>().state;

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        title: Text(
          '谁可以共享',
          style: TextStyle(
            color: Colors.black,
            fontSize: 16.sp,
            fontWeight: FontWeight.w500,
          ),
        ),
        centerTitle: true,
        leading: TextButton(
          onPressed: () => Get.back(),
          child: Text(
            '取消',
            style: TextStyle(
              color: Colors.grey.shade600,
              fontSize: 14.sp,
            ),
          ),
        ),
        actions: [
          TextButton(
            onPressed: () => logic.confirmSelection(),
            child: Text(
              '确定',
              style: TextStyle(
                color: Colors.blue,
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
      body: Column(
        children: [
          Divider(height: 10,thickness: 10, color: Colors.grey.shade300),
          Obx(() => _buildPrivacyOption(
            icon: Icons.visibility_off,
            title: '仅自己',
            isSelected: state.privacyType.value == PrivacyType.onlySelf,
            onTap: () => logic.setPrivacyType(PrivacyType.onlySelf),
          )),
          Divider(height: 1, color: Colors.grey.shade300),
          Obx(() => _buildPrivacyOption(
            icon: Icons.visibility,
            title: '所有成员',
            isSelected: state.privacyType.value == PrivacyType.allMembers,
            onTap: () => logic.setPrivacyType(PrivacyType.allMembers),
          )),
          Divider(height: 1, color: Colors.grey.shade300),
          Obx(() => _buildPrivacyOption(
            icon: Icons.visibility,
            title: '选择部分成员可见',
            isSelected: state.privacyType.value == PrivacyType.selectedMembers,
            showCheckmark: true,
            onTap: () => logic.setPrivacyType(PrivacyType.selectedMembers),
          )),
          Divider(height: 1, color: Colors.grey.shade300),
          Obx(() {
            if (state.privacyType.value == PrivacyType.selectedMembers) {
              return _buildMembersList(state, logic);
            } else {
              return SizedBox.shrink();
            }
          }),
          Expanded(child: _buildBottomPlaceholder()),
        ],
      ),
    );
  }

  Widget _buildPrivacyOption({
    required IconData icon,
    required String title,
    required bool isSelected,
    bool showCheckmark = false,
    required VoidCallback onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 40.w, vertical: 16.h),
        child: Row(
          children: [
            Icon(
              icon,
              color: isSelected ? Colors.blue : Colors.grey.shade600,
              size: 24.r,
            ),
            SizedBox(width: 12.w),
            Expanded(
              child: Text(
                title,
                style: TextStyle(
                  fontSize: 14.sp,
                  color: Colors.black87,
                ),
              ),
            ),
            if (showCheckmark && isSelected)
              Icon(
                Icons.check,
                color: Colors.blue,
                size: 24.r,
              ),
          ],
        ),
      ),
    );
  }

  Widget _buildMembersList(PrivacyState state, PrivacyLogic logic) {
    return Column(
      children: [
        for (var member in state.members)
          _buildMemberItem(member, logic),
      ],
    );
  }

  Widget _buildMemberItem(Member member, PrivacyLogic logic) {
    return InkWell(
      onTap: () => logic.toggleMemberSelection(member),
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 12.h),
        child: Row(
          children: [
            SizedBox(width: 40.w), // 缩进
            Container(
              width: 16.w,
              height: 16.h,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: member.isSelected ? Colors.blue : Colors.transparent,
                border: Border.all(
                  color: member.isSelected ? Colors.blue : Colors.grey.shade400,
                  width: 2.w,
                ),
              ),
              child: member.isSelected
                  ? Icon(
                      Icons.check,
                      color: Colors.white,
                      size: 12.r,
                    )
                  : null,
            ),
            SizedBox(width: 16.w),
            Container(
              width: 30.w,
              height: 30.h,
              decoration: BoxDecoration(
                color: Colors.grey.shade300,
                shape: BoxShape.circle,
              ),
            ),
            SizedBox(width: 12.w),
            Text(
              member.name,
              style: TextStyle(
                fontSize: 14.sp,
                color: Colors.black87,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildBottomPlaceholder() {
    return Container(
      color: Colors.grey.shade200,
    );
  }
} 